import java.awt.Color;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Vector;
import java.lang.reflect.Field;

import monarc.center.Activity;
import monarc.jscheduler.dag.DAGJob;
import monarc.jscheduler.dag.graph.Muchie;
import monarc.jscheduler.dag.graph.Nod;
import edu.uci.ics.jung.graph.impl.DirectedSparseGraph;

/**
 * @author Alexandra Olteanu
 *
 * General Activity form
 */

public class GeneralActivity extends Activity {

	Vector<Nod> vNod;	
	public DirectedSparseGraph taskuri; 
	
    /**
     * The constructor.
     */
    public GeneralActivity() {
    }
    
    /**
     * This method return a Color object associated with this specific color name
     * @param colorName color name
     * @return
     */
    public Color getColor(String colorName) {
        try {
            Field field = Class.forName("java.awt.Color").getField(colorName);
            return (Color)field.get(null);
        } catch (Exception e) {
            return null;
        }
    }
    
    /**
     * This method create the Direct sparse graph of tasks and a vector of Nod
     * @param inputFile
     * @throws java.io.IOException
     */
    void pushTasks(String inputFile)throws java.io.IOException{
    	
    	taskuri = new DirectedSparseGraph();
		vNod = new Vector<Nod>();
    	BufferedReader bfr =new BufferedReader(new FileReader(inputFile)) ;
		String line;
		String comp[];
		int nrNods;
    	
    	try{
    		line=bfr.readLine();
			comp=line.split(" ");
			nrNods=Integer.parseInt(comp[0]);
			
			for ( int i = 0; i < nrNods; i++ ) {
				vNod.add( new Nod() );
				Nod node = vNod.get(i);
				node = (Nod)taskuri.addVertex( vNod.get(i) );
				node.brushColor = Color.orange;
				node.penColor = Color.black;
				node.data = 0;
				node.index = i;			
			}	
			
			line=bfr.readLine();
			comp=line.split(" ");
			
			while(!line.startsWith("*")) {
				vNod.get(Integer.parseInt(comp[0])).data = Integer.parseInt(comp[1]);
				line=bfr.readLine();
				comp=line.split(" ");
				
			}

			line=bfr.readLine();
			comp=line.split(" ");
			
			while(!line.startsWith("*")) {
			
				Muchie mc = new Muchie( vNod.get(Integer.parseInt(comp[0])), vNod.get(Integer.parseInt(comp[1])));
				mc = (Muchie)taskuri.addEdge(mc);
				//mc.color = this.getColor(comp[3]);
				mc.data = Integer.parseInt(comp[2]);
				line=bfr.readLine();
				comp=line.split(" ");
			}	
    	}
    	catch (java.lang.Exception e)
		{
			System.out.println("Parametrii in fisier sunt gresiti! Verificati corectitudinea formatului!");
		}
    	
    }
    
    /**
     * Overrides the default behavior of pushJobs
     */
    public void pushJobs() {
    	try{
	    	pushTasks("InputFile_1.txt");
	    	DAGJob job = new DAGJob(taskuri);
	    	addJob(job);
	    	
    	}
    	catch(java.io.IOException e){
    		System.out.println("Fisier invalid!");
    	}
    }
}